package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-22
 */
@RestController
@RequestMapping("/eduservice/teacher")
@Slf4j
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public Result findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return Result.ok().data("items",list);
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/delete/{id}")
    public Result remove(@ApiParam(name = "id", value = "讲师ID", required = true)
                               @PathVariable(value = "id")String id){
        boolean boo = eduTeacherService.removeById(id);
        log.info("逻辑删除讲师 ===>结果：{} ===>id：{}",boo,id);
        if (boo) { //如果为true
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     *      分页查询
     * @param current
     * @param limit
     * @return
     */
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public Result PageTeacher(@PathVariable Integer current,@PathVariable Integer limit) {
        Page<EduTeacher> page = new Page<>(current,limit);
        eduTeacherService.page(page, null);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        //records: 数据结果显示
        return Result.ok().data("total",total).data("rows",records);
    }


    //分页条件查询
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public Result pageTeacherCondition(@PathVariable Long current, @PathVariable Long limit,
                                     @RequestBody(required = false) TeacherQuery teacherQuery) {
                                      //@RequestBody封装对象的json数据到前端，required表示值可以没有,true表示必须有
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        eduTeacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return Result.ok().data("total",total).data("rows",records);
    }


    //添加讲师接口方法
    @PostMapping("/addTeacher")
    public Result add(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        log.info("插入的讲师数据: ==>结果集:{}, ==>布尔值:{}",eduTeacher,save);
        if (save) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    //根据讲师id查询
    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@PathVariable String id) {
        EduTeacher teacher = eduTeacherService.getById(id);
//        try {
//            int a = 10/0;
//        } catch (Exception e) {
//            throw new GuliException(201,"执行了自定义异常处理");
//        }
        log.info("根据id查询讲师 ==>{}",teacher);
        return Result.ok().data("teacher",teacher);
    }
    //修改功能
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        log.info("根据teacher修改数据 ==>结果集:{} ==>布尔值:{}",eduTeacher,flag);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    public static void main(String[] args) {
    }
}

